# 导包
# 创建链接
import pymysql

try:
    # 创建链接
    conn = pymysql.connect(host="211.103.136.244",
                           port=7061,
                           user="student",
                           password="iHRM_student_2021",
                           database="test_db",
                           charset="utf8")
    # 创建游标
    cursor = conn.cursor()
    # 执行sql
    # 新增一条图书记录
    sql="insert into t_book(id,title,pub_date) values(508,'红楼梦','1981-01-01')"
    # 修改图书名称
    # sql = "update t_book set `read`=`read`+ 1 where id = 508;"
    # 删除图书馆记录
    # sql="delete from t_book where id = 508;"
    # 提交事务
    conn.commit()

except Exception as e:
    # 回滚事务
    conn.rollback()
    # 打印异常信息
    print(e)
finally:
    cursor.close()
    conn.close()
